package com.bestcem.xm.award.dao.impl.mysql;

import com.alibaba.fastjson.JSON;
import com.bestcem.xm.award.controller.convert.AwardConvert;
import com.bestcem.xm.award.dao.AwardAwardDao;
import com.bestcem.xm.award.dao.mapper.*;
import com.bestcem.xm.award.dao.mapper.query.ListAwardQuery;
import com.bestcem.xm.award.dao.mapper.query.UpdateAwardQuery;
import com.bestcem.xm.award.entity.mysql.*;
import com.bestcem.xm.award.entity.pojo.AwardAwardDO;
import com.bestcem.xm.award.enums.AwardStatusEnum;
import com.bestcem.xm.award.enums.AwardTypeEnum;
import com.bestcem.xm.award.enums.DeviceTypeEnum;
import com.bestcem.xm.award.enums.RecordFlagStatusEnum;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.common.core.uitls.EnumUtil;
import com.bestcem.xm.common.core.uitls.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 奖励数据mysql实现dao
 *
 * @author sh.pei <sh.pei@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/23 11:25
 */
@Repository
@Slf4j
public class AwardDaoImpl implements AwardAwardDao {

    @Autowired
    private AwardEntityMapper awardEntityMapper;

    @Autowired
    private AwardBandPointEntityMapper awardBandPointEntityMapper;

    @Autowired
    private AwardCustomEntityMapper awardCustomEntityMapper;

    @Autowired
    private AwardCustomAttachmentEntityMapper awardCustomAttachmentEntityMapper;

    @Autowired
    private AwardDrawEntityMapper awardDrawEntityMapper;

    @Autowired
    private AwardDrawAwardEntityMapper awardDrawAwardEntityMapper;

    @Autowired
    private AwardRedPacketEntityMapper awardRedPacketEntityMapper;

    @Autowired
    private AwardRuleEntityMapper awardRuleEntityMapper;

    @Autowired
    private AwardThirdPartyEntityMapper awardThirdPartyEntityMapper;

    @Autowired
    private AwardVoucherEntityMapper awardVoucherEntityMapper;


    @Override
    public AwardAwardDO findById(String awardId) {
        AwardEntity awardEntity = findAwardEntityById(awardId);
        if (awardEntity == null) {
            return null;
        }
        AwardAwardDO awardAwardDO = AwardConvert.awardEntityToAwardAwardDO(awardEntity, null);
        AwardRuleEntity ruleEntity = awardRuleEntityMapper.selectByPrimaryKey(awardId);
        awardAwardDO = AwardConvert.awardRuleEntityToAwardAwardDO(ruleEntity, awardAwardDO);


        AwardTypeEnum enumByIndex = EnumUtil.getEnumByIndex(AwardTypeEnum.class, awardEntity.getType());
        switch (enumByIndex) {
            case WECHAT:
            case ALIPAY:
                awardAwardDO = getAwardVoucher(awardId, awardAwardDO);
                break;
            case REMOTE:
                awardAwardDO = getAwardThirdParty(awardId, awardAwardDO);
                break;
            case DRAW:
            case NEW_DRAW:
                awardAwardDO = getAwardDraw(awardId, awardAwardDO);
                break;
            case CUSTOM:
                awardAwardDO = getAwardCustom(awardId, awardAwardDO);
                break;
            case REDPACKET:
                awardAwardDO = getAwardRedPacket(awardId, awardAwardDO);
                break;
            case POINT:
                awardAwardDO = getAwardBandPoint(awardId, awardAwardDO);
                break;
            default:
                break;
        }

        return awardAwardDO;
    }

    /**
     * 根据id查询奖励信息
     *
     * @param awardId
     * @return
     */
    private AwardEntity findAwardEntityById(String awardId) {
        AwardEntity awardEntity = awardEntityMapper.selectByPrimaryKey(awardId);
        if (awardEntity == null || !Objects.equals(awardEntity.getRecordFlag(), RecordFlagStatusEnum.ACTIVE.getIndex())) {
            return null;
        }
        return awardEntity;
    }

    /**
     * 填充卡券奖励信息
     *
     * @param awardId
     * @param awardAwardDO
     * @return
     */
    private AwardAwardDO getAwardVoucher(String awardId, AwardAwardDO awardAwardDO) {
        List<AwardVoucherEntity> voucherEntityList = new ArrayList<>();
        AwardVoucherEntity phone = awardVoucherEntityMapper.selectByPrimaryKey(awardId, DeviceTypeEnum.MOBILE.getIndex());
        if (phone != null) {
            voucherEntityList.add(phone);
        }
        AwardVoucherEntity pc = awardVoucherEntityMapper.selectByPrimaryKey(awardId, DeviceTypeEnum.PC.getIndex());
        if (pc != null) {
            voucherEntityList.add(pc);
        }
        awardAwardDO = AwardConvert.awardVoucherEntityToAwardAwardDO(voucherEntityList, awardAwardDO);
        return awardAwardDO;
    }

    /**
     * 填充第三方奖励信息
     *
     * @param awardId
     * @param awardAwardDO
     * @return
     */
    private AwardAwardDO getAwardThirdParty(String awardId, AwardAwardDO awardAwardDO) {
        AwardThirdPartyEntity thirdPartyEntity = awardThirdPartyEntityMapper.selectByPrimaryKey(awardId);
        awardAwardDO = AwardConvert.awardThirdPartyEntityToAwardAwardDO(thirdPartyEntity, awardAwardDO);
        return awardAwardDO;
    }

    /**
     * 填充抽奖奖励信息
     *
     * @param awardId
     * @param awardAwardDO
     * @return
     */
    private AwardAwardDO getAwardDraw(String awardId, AwardAwardDO awardAwardDO) {
        AwardDrawEntity awardDrawEntity = awardDrawEntityMapper.selectByPrimaryKey(awardId);
        awardAwardDO = AwardConvert.awardDrawEntityToAwardAwardDO(awardDrawEntity, awardAwardDO);

        List<AwardDrawAwardEntity> drawAwardEntityList = awardDrawAwardEntityMapper.selectByAwardId(awardId);
        awardAwardDO = AwardConvert.awardDrawAwardEntityToAwardAwardDO(drawAwardEntityList, awardAwardDO);

        return awardAwardDO;
    }

    /**
     * 填充定制奖励信息
     *
     * @param awardId
     * @param awardAwardDO
     * @return
     */
    private AwardAwardDO getAwardCustom(String awardId, AwardAwardDO awardAwardDO) {
        AwardCustomEntity awardCustomEntity = awardCustomEntityMapper.selectByPrimaryKey(awardId);
        awardAwardDO = AwardConvert.awardCustomEntityToAwardAwardDO(awardCustomEntity, awardAwardDO);

        List<AwardCustomAttachmentEntity> awardCustomAttachmentEntities = awardCustomAttachmentEntityMapper.selectByAwardId(awardId);
        awardAwardDO = AwardConvert.awardCustomAttachmentEntityToAwardAwardDO(awardCustomAttachmentEntities, awardAwardDO);

        return awardAwardDO;
    }

    /**
     * 填充微信红包奖励信息
     *
     * @param awardId
     * @param awardAwardDO
     * @return
     */
    private AwardAwardDO getAwardRedPacket(String awardId, AwardAwardDO awardAwardDO) {
        AwardRedPacketEntity redPacketEntity = awardRedPacketEntityMapper.selectByPrimaryKey(awardId);
        awardAwardDO = AwardConvert.awardRedPacketEntityToAwardAwardDO(redPacketEntity, awardAwardDO);

        return awardAwardDO;
    }

    /**
     * 填充积分奖励信息
     *
     * @param awardId
     * @param awardAwardDO
     * @return
     */
    private AwardAwardDO getAwardBandPoint(String awardId, AwardAwardDO awardAwardDO) {
        AwardBandPointEntity awardBandPointEntity = awardBandPointEntityMapper.selectByPrimaryKey(awardId);
        awardAwardDO = AwardConvert.awardBandPointEntityToAwardAwardDO(awardBandPointEntity, awardAwardDO);

        return awardAwardDO;
    }

    @Override
    public AwardAwardDO startAward(String awardId, Date startTime) {
        AwardEntity awardEntity = new AwardEntity();
        awardEntity.setAwardId(awardId);
        awardEntity.setStartTime(startTime);
        awardEntity.setStatus(AwardStatusEnum.ACTIVE.getIndex());
        awardEntity.setActivated(EnumUtil.booleanToInt(Boolean.TRUE));
        awardEntity.setUpdateTime(DateUtil.getCommonDate());
        int count = awardEntityMapper.startAward(awardEntity);
        if (count == 0) {
            return null;
        }
        return findById(awardId);
    }

    @Override
    public AwardAwardDO stopAward(String awardId, Date endTime) {
        AwardEntity awardEntity = new AwardEntity();
        awardEntity.setAwardId(awardId);
        awardEntity.setEndTime(endTime);
        awardEntity.setStatus(AwardStatusEnum.FINISH.getIndex());
        awardEntity.setUpdateTime(DateUtil.getCommonDate());
        int count = awardEntityMapper.stopAward(awardEntity);
        if (count == 0) {
            return null;
        }
        return findById(awardId);
    }

    @Override
    public AwardAwardDO startDrawAward(String awardId, Date startTime) {
        return startAward(awardId, startTime);
    }

    @Override
    public AwardAwardDO stopDrawAward(String awardId, Date stopTime) {
        return stopAward(awardId, stopTime);
    }

    @Override
    public AwardAwardDO startBonusAward(String awardId, Date startTime) {
        return startAward(awardId, startTime);
    }

    @Override
    public AwardAwardDO stopBonusAward(String awardId, Date stopTime) {
        return stopAward(awardId, stopTime);
    }

    @Override
    public AwardAwardDO findNotStartBonus(String awardId, Date startTime) {
        AwardAwardDO awardAwardDO = findById(awardId);
        if (awardAwardDO == null || !Objects.equals(AwardTypeEnum.REDPACKET.getIndex(), awardAwardDO.getTType()) || !Objects.equals(startTime, awardAwardDO.getBasic().getStartTime()) || !Objects.equals(AwardStatusEnum.INACTIVE.getIndex(), awardAwardDO.getStatus())) {
            return null;
        }
        return awardAwardDO;
    }

    @Override
    public AwardAwardDO findByAwardIdAndStatus(String awardId, Integer status) {
        AwardAwardDO awardAwardDO = findById(awardId);
        if (awardAwardDO == null || !Objects.equals(status, awardAwardDO.getStatus())) {
            return null;
        }
        return awardAwardDO;
    }

    @Override
    public AwardAwardDO findByAwardIdAndOrgId(String awardId, String orgId) {
        AwardAwardDO awardAwardDO = findById(awardId);
        if (awardAwardDO == null || !equalsIgnoreCase(orgId, awardAwardDO.getOrgId())) {
            return null;
        }
        return awardAwardDO;
    }

    private boolean equalsIgnoreCase(String str1, String str2) {
        if (str1 == null) {
            return str2 == null;
        } else {
            return str1.equalsIgnoreCase(str2);
        }
    }

    @Override
    public AwardAwardDO findByAwardIdAndOrgIdAndType(String awardId, String orgId, Integer tType) {
        AwardAwardDO awardAwardDO = findById(awardId);
        if (awardAwardDO == null || !equalsIgnoreCase(orgId, awardAwardDO.getOrgId()) || !Objects.equals(tType, awardAwardDO.getTType())) {
            return null;
        }
        return awardAwardDO;
    }

    @Override
    public List<AwardAwardDO> listAwardAwardByQdesIdAndStatus(String orgId, String projectId, List<Integer> statuses) {
        return listPageAwardAward(orgId, projectId, null, statuses, null, null);
    }

    private ListAwardQuery createListQuery(String orgId, String projectId, List<Integer> tTypes, List<Integer> statuses, Integer page, Integer size) {
        ListAwardQuery listAwardQuery = new ListAwardQuery();
        listAwardQuery.setOrgId(orgId);
        listAwardQuery.setProjectId(projectId);
        if (tTypes != null && tTypes.size() == 1) {
            listAwardQuery.setType(tTypes.get(0));
        } else {
            listAwardQuery.setTypes(tTypes);
        }
        if (statuses != null && statuses.size() == 1) {
            listAwardQuery.setStatus(statuses.get(0));
        } else {
            listAwardQuery.setStatuses(statuses);
        }
        if (page != null && size != null) {
            listAwardQuery.setSkip(Math.max(page - 1, 0) * size);
            listAwardQuery.setSize(size);
        }
        return listAwardQuery;
    }

    @Override
    public List<AwardAwardDO> listPageAwardAward(String orgId, String projectId, List<Integer> tTypes, List<Integer> statuses, Integer page, Integer size) {
        ListAwardQuery listAwardQuery = createListQuery(orgId, projectId, tTypes, statuses, page, size);

        List<AwardEntity> awardEntities = awardEntityMapper.listAwards(listAwardQuery);
        if (CollectionUtils.isEmpty(awardEntities)) {
            return new ArrayList<>();
        }
        List<AwardAwardDO> awardDOS = awardEntities.stream().map(awardEntity -> AwardConvert.awardEntityToAwardAwardDO(awardEntity, null)).collect(Collectors.toList());
        return awardDOS;
    }

    @Override
    public long countAwardAward(String orgId, String projectId, List<Integer> tTypes, List<Integer> statuses) {
        ListAwardQuery listAwardQuery = createListQuery(orgId, projectId, tTypes, statuses, null, null);
        int count = awardEntityMapper.countAwards(listAwardQuery);
        return count;
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public AwardAwardDO save(AwardAwardDO awardAwardDO) {
        // 更新
        if (awardAwardDO != null && awardAwardDO.getId() != null && findAwardEntityById(awardAwardDO.getId()) != null) {
            updateByAwardId(awardAwardDO.getId(), awardAwardDO);
        } // 创建
        else {
            if (awardAwardDO.getId() == null) {
                awardAwardDO.setId(StringUtil.getId());
            }
            AwardEntity awardEntity = AwardConvert.awardAwardDOToAwardEntity(awardAwardDO);
            awardEntity.setRecordFlag(RecordFlagStatusEnum.ACTIVE.getIndex());
            awardEntity.setCreateTime(DateUtil.getCommonDate());
            awardEntity.setUpdateTime(DateUtil.getCommonDate());
            awardEntityMapper.insertSelective(awardEntity);

            AwardRuleEntity ruleEntity = AwardConvert.awardAwardDOToAwardRuleEntity(awardAwardDO);
            if (ruleEntity != null) {
                ruleEntity.setCreateTime(DateUtil.getCommonDate());
                ruleEntity.setUpdateTime(DateUtil.getCommonDate());
                awardRuleEntityMapper.insertSelective(ruleEntity);
            }

            updateAwardUniqueData(awardAwardDO, true);


        }

        return findById(awardAwardDO.getId());
    }

    /**
     * 更新各个奖励的明确的信息
     *
     * @param awardAwardDO
     * @param save         如果是true，则删除并创建； 否则进行更新策略
     */
    private void updateAwardUniqueData(AwardAwardDO awardAwardDO, boolean save) {
        if (log.isInfoEnabled()) {
            log.info("[award] updateAwardUniqueData: awardAwardDO:{}, save:{}", JSON.toJSONString(awardAwardDO), save);
        }
        AwardRuleEntity ruleEntity = AwardConvert.awardAwardDOToAwardRuleEntity(awardAwardDO);
        if (save) {
            awardRuleEntityMapper.deleteByPrimaryKey(awardAwardDO.getId());
            if (ruleEntity != null) {
                ruleEntity.setCreateTime(DateUtil.getCommonDate());
                ruleEntity.setUpdateTime(DateUtil.getCommonDate());
                awardRuleEntityMapper.insertSelective(ruleEntity);
            }
        } else {
            if (ruleEntity != null) {
                ruleEntity.setUpdateTime(DateUtil.getCommonDate());
                awardRuleEntityMapper.updateByPrimaryKeySelective(ruleEntity);
            }
        }

        AwardTypeEnum enumByIndex = EnumUtil.getEnumByIndex(AwardTypeEnum.class, awardAwardDO.getTType());
        switch (enumByIndex) {
            case WECHAT:
            case ALIPAY:
                List<AwardVoucherEntity> awardVoucherEntityList = AwardConvert.awardAwardDOToAwardVoucherEntity(awardAwardDO);
                if (save) {
                    awardVoucherEntityMapper.deleteByPrimaryKey(awardAwardDO.getId(), DeviceTypeEnum.PC.getIndex());
                    awardVoucherEntityMapper.deleteByPrimaryKey(awardAwardDO.getId(), DeviceTypeEnum.MOBILE.getIndex());
                    if (!CollectionUtils.isEmpty(awardVoucherEntityList)) {
                        for (AwardVoucherEntity awardVoucherEntity : awardVoucherEntityList) {
                            awardVoucherEntity.setCreateTime(DateUtil.getCommonDate());
                            awardVoucherEntity.setUpdateTime(DateUtil.getCommonDate());
                            awardVoucherEntityMapper.insertSelective(awardVoucherEntity);
                        }
                    }
                } else {
                    if (!CollectionUtils.isEmpty(awardVoucherEntityList)) {
                        for (AwardVoucherEntity awardVoucherEntity : awardVoucherEntityList) {
                            awardVoucherEntity.setUpdateTime(DateUtil.getCommonDate());
                            awardVoucherEntityMapper.updateByPrimaryKeySelective(awardVoucherEntity);
                        }
                    }
                }
                break;
            case REMOTE:
                AwardThirdPartyEntity thirdPartyEntity = AwardConvert.awardAwardDOToAwardThirdPartyEntity(awardAwardDO);
                if (save) {
                    awardThirdPartyEntityMapper.deleteByPrimaryKey(awardAwardDO.getId());
                    if (thirdPartyEntity != null) {
                        thirdPartyEntity.setCreateTime(DateUtil.getCommonDate());
                        thirdPartyEntity.setUpdateTime(DateUtil.getCommonDate());
                        awardThirdPartyEntityMapper.insertSelective(thirdPartyEntity);
                    }
                } else {
                    if (thirdPartyEntity != null) {
                        thirdPartyEntity.setUpdateTime(DateUtil.getCommonDate());
                        awardThirdPartyEntityMapper.updateByPrimaryKeySelective(thirdPartyEntity);
                    }
                }

                break;
            case DRAW:
            case NEW_DRAW:
                AwardDrawEntity awardDrawEntity = AwardConvert.awardAwardDOToAwardDrawEntity(awardAwardDO);
                List<AwardDrawAwardEntity> drawAwardEntityList = AwardConvert.awardAwardDOToAwardDrawAwardEntity(awardAwardDO);
                if (save) {
                    awardDrawEntityMapper.deleteByPrimaryKey(awardAwardDO.getId());
                    if (awardDrawEntity != null) {
                        awardDrawEntity.setCreateTime(DateUtil.getCommonDate());
                        awardDrawEntity.setUpdateTime(DateUtil.getCommonDate());
                        awardDrawEntityMapper.insertSelective(awardDrawEntity);
                    }

                    awardDrawAwardEntityMapper.deleteByAwardId(awardAwardDO.getId());
                    if (drawAwardEntityList != null) {
                        for (AwardDrawAwardEntity drawAwardEntity : drawAwardEntityList) {
                            drawAwardEntity.setCreateTime(DateUtil.getCommonDate());
                            drawAwardEntity.setUpdateTime(DateUtil.getCommonDate());
                            awardDrawAwardEntityMapper.insertSelective(drawAwardEntity);
                        }
                    }
                } else {
                    if (awardDrawEntity != null) {
                        awardDrawEntity.setUpdateTime(DateUtil.getCommonDate());
                        awardDrawEntityMapper.updateByPrimaryKeySelective(awardDrawEntity);
                    }
                    if (!CollectionUtils.isEmpty(drawAwardEntityList)) {
                        awardDrawAwardEntityMapper.deleteByAwardId(awardAwardDO.getId());
                        for (AwardDrawAwardEntity drawAwardEntity : drawAwardEntityList) {
                            drawAwardEntity.setCreateTime(DateUtil.getCommonDate());
                            drawAwardEntity.setUpdateTime(DateUtil.getCommonDate());
                            awardDrawAwardEntityMapper.insertSelective(drawAwardEntity);
                        }
                    }
                }

                break;
            case CUSTOM:
                AwardCustomEntity awardCustomEntity = AwardConvert.awardAwardDOToAwardCustomEntity(awardAwardDO);
                List<AwardCustomAttachmentEntity> awardCustomAttachmentEntities = AwardConvert.awardAwardDOToAwardCustomAttachmentEntity(awardAwardDO);
                if (save) {
                    awardCustomEntityMapper.deleteByPrimaryKey(awardAwardDO.getId());
                    if (awardCustomEntity != null) {
                        awardCustomEntity.setCreateTime(DateUtil.getCommonDate());
                        awardCustomEntity.setUpdateTime(DateUtil.getCommonDate());
                        awardCustomEntityMapper.insertSelective(awardCustomEntity);
                    }

                    awardCustomAttachmentEntityMapper.deleteByAwardId(awardAwardDO.getId());
                    if (awardCustomAttachmentEntities != null) {
                        for (AwardCustomAttachmentEntity customAttachmentEntity : awardCustomAttachmentEntities) {
                            customAttachmentEntity.setCreateTime(DateUtil.getCommonDate());
                            customAttachmentEntity.setUpdateTime(DateUtil.getCommonDate());
                            awardCustomAttachmentEntityMapper.insertSelective(customAttachmentEntity);
                        }
                    }
                } else {
                    if (awardCustomEntity != null) {
                        awardCustomEntity.setUpdateTime(DateUtil.getCommonDate());
                        awardCustomEntityMapper.updateByPrimaryKeySelective(awardCustomEntity);
                    }
                    if (awardCustomAttachmentEntities != null) {
                        for (AwardCustomAttachmentEntity customAttachmentEntity : awardCustomAttachmentEntities) {
                            customAttachmentEntity.setUpdateTime(DateUtil.getCommonDate());
                            awardCustomAttachmentEntityMapper.updateByPrimaryKeySelective(customAttachmentEntity);
                        }
                    }
                }

                break;
            case REDPACKET:
                AwardRedPacketEntity redPacketEntity = AwardConvert.awardAwardDOToAwardRedPacketEntity(awardAwardDO);
                if (log.isInfoEnabled()) {
                    log.info("[award] 红包配置信息:{}", JSON.toJSONString(redPacketEntity));
                }
                if (save) {
                    awardRedPacketEntityMapper.deleteByPrimaryKey(awardAwardDO.getId());
                    if (redPacketEntity != null) {
                        redPacketEntity.setCreateTime(DateUtil.getCommonDate());
                        redPacketEntity.setUpdateTime(DateUtil.getCommonDate());
                        awardRedPacketEntityMapper.insertSelective(redPacketEntity);
                    }
                } else {
                    if (redPacketEntity != null) {
                        redPacketEntity.setUpdateTime(DateUtil.getCommonDate());
                        awardRedPacketEntityMapper.updateByPrimaryKeySelective(redPacketEntity);
                    }
                }

                break;
            case POINT:
                AwardBandPointEntity awardBandPointEntity = AwardConvert.awardAwardDOToAwardBandPointEntity(awardAwardDO);
                if (save) {
                    awardBandPointEntityMapper.deleteByPrimaryKey(awardAwardDO.getId());
                    if (awardBandPointEntity != null) {
                        awardBandPointEntity.setCreateTime(DateUtil.getCommonDate());
                        awardBandPointEntity.setUpdateTime(DateUtil.getCommonDate());
                        awardBandPointEntityMapper.insertSelective(awardBandPointEntity);
                    }
                } else {
                    if (awardBandPointEntity != null) {
                        awardBandPointEntity.setUpdateTime(DateUtil.getCommonDate());
                        awardBandPointEntityMapper.updateByPrimaryKeySelective(awardBandPointEntity);
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int updateByAwardId(String awardId, AwardAwardDO awardAwardDO) {
        awardAwardDO.setId(awardId);
        AwardEntity awardEntity = AwardConvert.awardAwardDOToAwardEntity(awardAwardDO);
        awardEntity.setUpdateTime(DateUtil.getCommonDate());
        int count = awardEntityMapper.updateByPrimaryKeySelective(awardEntity);
        if (count == 0) {
            return count;
        }
        awardAwardDO.setTType(findAwardEntityById(awardId).getType());
        updateAwardUniqueData(awardAwardDO, false);
        return 1;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int updateByIdAndStatus(String awardId, Integer status, AwardAwardDO awardAwardDO) {
        awardAwardDO.setId(awardId);
        AwardEntity awardEntity = AwardConvert.awardAwardDOToAwardEntity(awardAwardDO);
        UpdateAwardQuery updateAwardQuery = new UpdateAwardQuery();
        BeanUtils.copyProperties(awardEntity, updateAwardQuery);
        updateAwardQuery.setOldStatus(status);
        updateAwardQuery.setUpdateTime(DateUtil.getCommonDate());
        int count = awardEntityMapper.updateByPrimaryKeyAndOldStatusSelective(updateAwardQuery);
        if (count == 0) {
            return count;
        }
        awardAwardDO.setTType(findAwardEntityById(awardId).getType());
        updateAwardUniqueData(awardAwardDO, false);
        return 1;
    }

    @Override
    public AwardAwardDO findById(String awardId, String[] includeFields, String excludeFields) {
        return findById(awardId);
    }

    @Override
    public AwardAwardDO findByIdAndStatusAndType(String awardId, Integer status, Integer type, String[] includeFields, String excludeFields) {
        ListAwardQuery listAwardQuery = new ListAwardQuery();
        listAwardQuery.setAwardId(awardId);
        listAwardQuery.setStatus(status);
        listAwardQuery.setType(type);
        List<AwardEntity> awardEntities = awardEntityMapper.listAwards(listAwardQuery);
        if (CollectionUtils.isEmpty(awardEntities)) {
            return null;
        }
        AwardAwardDO awardAwardDO = findById(awardId);
        if (awardAwardDO == null) {
            return null;
        }
        awardAwardDO = AwardConvert.awardEntityToAwardAwardDO(awardEntities.get(0), awardAwardDO);
        return awardAwardDO;
    }

    @Override
    public AwardAwardDO deleteAttachmentByAttachmentIdAndAwardId(String attachmentId, String awardId) {
        int deleteCount = awardCustomAttachmentEntityMapper.deleteByPrimaryKey(awardId, attachmentId);
        if (deleteCount == 0) {
            return null;
        }
        AwardAwardDO awardDO = findById(awardId);
        return awardDO;
    }

    @Override
    public AwardAwardDO findByAttachmentIdAndAwardId(String attachmentId, String awardId) {
        AwardCustomAttachmentEntity attachmentEntity = awardCustomAttachmentEntityMapper.selectByPrimaryKey(awardId, attachmentId);
        if (attachmentEntity == null) {
            return null;
        }
        AwardAwardDO awardDO = findById(awardId);
        return awardDO;
    }

    @Override
    public List<Integer> queryDistinctAwardLevel(String awardId, String orgId) {
        AwardEntity awardEntity = findAwardEntityById(awardId);
        if (awardEntity == null || !equalsIgnoreCase(awardEntity.getOrgId(), orgId)) {
            return new ArrayList<>();
        }
        List<Integer> levels = awardDrawAwardEntityMapper.queryDistinctAwardLevel(awardId);
        return levels;
    }

    @Override
    public List<String> queryDistinctAwardName(String awardId, String orgId) {
        AwardEntity awardEntity = findAwardEntityById(awardId);
        if (awardEntity == null || !equalsIgnoreCase(awardEntity.getOrgId(), orgId)) {
            return new ArrayList<>();
        }
        List<String> levels = awardDrawAwardEntityMapper.queryDistinctAwardName(awardId);
        return levels;
    }

    @Override
    public int updateCurCountAddOneById(String awardId) {
        int count = awardEntityMapper.updateCurCountAddOneById(awardId, DateUtil.getCommonDate());
        return count;
    }

    @Override
    public Long findDuplicateName(String name, String projectId, String awardId) {
        int count = awardEntityMapper.findDuplicateName(name, projectId, awardId);
        return Long.valueOf(count);
    }

    @Override
    public int remove(String awardId) {
        int flag = awardEntityMapper.remove(awardId, DateUtil.getCommonDate());
        return flag;
    }

    @Override
    public boolean existsNotDeleteAwardByPointId(String bandPointId) {
        int count = awardBandPointEntityMapper.existsNotDeleteAwardByPointId(bandPointId);
        return count > 0;
    }

    @Override
    public List<AwardAwardDO> listAwardsByIds(List<String> awardIds) {
        if (CollectionUtils.isEmpty(awardIds)) {
            return new ArrayList<>();
        }
        ListAwardQuery listAwardQuery = new ListAwardQuery();
        listAwardQuery.setAwardIds(awardIds);
        List<AwardEntity> awardEntities = awardEntityMapper.listAwards(listAwardQuery);
        if (CollectionUtils.isEmpty(awardEntities)) {
            return new ArrayList<>();
        }
        List<AwardAwardDO> ret = awardEntities.stream().map(awardEntity -> {
            return AwardConvert.awardEntityToAwardAwardDO(awardEntity, null);
        }).collect(Collectors.toList());
        return ret;
    }

    @Override
    public List<AwardAwardDO> listAwardsByStartAndEndDay(Date startBeforeDay, Date endAfterDay, Integer page, Integer size) {
        ListAwardQuery listAwardQuery = new ListAwardQuery();
        if (page != null && size != null) {
            listAwardQuery.setSkip(Math.max(page - 1, 0) * size);
            listAwardQuery.setSize(size);
        }
        listAwardQuery.setStartBeforeDay(startBeforeDay);
        listAwardQuery.setEndAfterDay(endAfterDay);
        List<AwardEntity> awardEntities = awardEntityMapper.listAwards(listAwardQuery);
        if (CollectionUtils.isEmpty(awardEntities)) {
            return new ArrayList<>();
        }
        List<AwardAwardDO> ret = awardEntities.stream().map(awardEntity -> {
            return AwardConvert.awardEntityToAwardAwardDO(awardEntity, null);
        }).collect(Collectors.toList());
        return ret;
    }

    @Override
    public int batchFinishAwardByIds(List<String> awardIds) {
        return awardEntityMapper.batchFinishAwardByIds(awardIds, DateUtil.getCommonDate());
    }
}
